package com.xyrl.project.app.controller.csr;

import com.xyrl.common.util.ObjectUtil;
import com.xyrl.project.app.util.ShiroUtil;
import com.xyrl.project.common.base.vo.ResponseData;
import com.xyrl.project.common.csr.vo.CsrApplyVo;
import com.xyrl.project.common.store.entity.StoreLegal;
import com.xyrl.project.common.store.enu.StoreStatus;
import com.xyrl.project.common.store.service.StoreAuthService;
import com.xyrl.project.common.store.service.StoreLegalService;
import com.xyrl.project.common.store.service.StoreService;
import com.xyrl.project.common.store.vo.StoreSalesVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Arrays;

/**
 *  * @className CsrStoreController
 *  * @company 星纭融链科技服务有限公司
 *  * @author 徐恩源
 *  * @version 1.0
 *  * @date 2020-01-05 11:30:36
 *  * @Description 
 *  
 */
@RestController
@RequestMapping("/csr/store")
@Api(tags = "商户模块")
public class CsrStoreController {
    @Autowired
    private StoreService storeService;
    @Autowired
    private StoreAuthService storeAuthService;
    @Autowired
    private StoreLegalService storeLegalService;

    /**
     * 申请（进货商授信、供货商收款）
     *
     * @param applyVo
     * @author 徐恩源
     * @date 2020-01-05 11:30:36
     */
    @ApiOperation(value = "申请信息")
    @PostMapping("/apply")
    public ResponseData apply(@Valid CsrApplyVo applyVo) {
        long storeId = storeService.applyStore(ShiroUtil.loginUserId(), applyVo);
        return ResponseData.success(storeId);
    }

    /**
     * 查询商户信息
     *
     * @author 徐恩源
     * @date 2020-01-05 14:55:22
     */
    @ApiOperation(value = "我的模块，商户信息")
    @GetMapping("/info")
    public ResponseData<StoreSalesVo> info() {
        StoreSalesVo storeSalesVo = storeService.findStoreSalesVoById(ShiroUtil.loginUserId());
        // 身份证信息
        if (null != storeSalesVo && StoreStatus.follow.equals(storeSalesVo.getStoreVo().getStatus())) {
            StoreLegal storeLegal = storeLegalService.findStoreLegalByStoreId(storeSalesVo.getStoreVo().getId());
            storeSalesVo.setStoreLegal(storeLegal);
        }
        return ResponseData.success(storeSalesVo);
    }

    /**
     * 实名认证-上传身份证
     *
     * @param storeLegal
     * @return
     */
    @ApiOperation(value = "实名认证-上传身份证（实名认证S1）")
    @PostMapping("/idCardAuth")
    public ResponseData idCardAuth(@Valid StoreLegal storeLegal) {
        String result = storeLegalService.bindIdCard(storeLegal, null, false);
        if (ObjectUtil.isNotEmpty(result)) {
            return ResponseData.error(result);
        }
        return ResponseData.success();
    }

    /**
     * 实名认证-人脸识别
     *
     * @param storeId
     * @param attIds
     * @author 徐恩源
     * @date 2020-01-05 14:02:10
     */
    @ApiOperation(value = "实名认证-人脸识别（实名认证s2）")
    @PostMapping("/faceAuth")
    public ResponseData faceAuth(Long storeId, Long[] attIds) {
        String result = storeAuthService.faceAuth(storeId, null, Arrays.asList(attIds), false);
        if (ObjectUtil.isNotEmpty(result)) {
            return ResponseData.error(result);
        }

        return ResponseData.success();
    }

    /**
     * 修改店铺头像
     *
     * @param storeId 店铺id
     * @param path    头像地址
     * @return
     */
    @ApiOperation(value = "修改店铺头像")
    @PostMapping("/updateStoreLogo")
    public ResponseData updateStoreLogo(Long storeId, String path) {
        storeService.updateStoreLogo(ShiroUtil.loginUserId(), storeId, path);
        return ResponseData.success("修改店铺头像成功");
    }

}
